package com.ymatou.iapi.userregister.testcase;

import java.io.IOException;
import java.security.interfaces.RSAPublicKey;
import java.util.Base64;
import java.util.List;
import java.util.Random;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.auth.parameter.SignAuthBean;
import com.ymatou.iapi.userregister.parameter.SellerRegisterBean;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.SellerRegisterCall;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.core.tool.RSAUtils;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.LoginLog;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.model.Userinfoex;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.DesUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*直播微店买手注册
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SellerRegister{
	private static SellerRegisterBean sellerregisterBean;
	private static SellerRegisterCall sellerregisterCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播微店买手注册");
	}

	@Before
	public void caseUp() {
		sellerregisterBean=new SellerRegisterBean();
		sellerregisterCall=new SellerRegisterCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sellerregisterCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private SellerRegisterBean initSellerReg() {
		SellerRegisterBean srb=new SellerRegisterBean();
		srb.setClientIp(TestDataManager.getRandomIp());
		srb.setDid(TestDataManager.getRandomUUID().toString());
		srb.setLid(TestDataManager.getRandomUUID().toString());
		srb.setLoginId("sr"+YMTDateUtil.getSysTime("MMddHHmmssSSS"));
		srb.setLoginMobile(TestDataManager.getPhoneNum("11"));
		srb.setPassword(JUserCallService.defaultLoginPwd);
		srb.setRegSource(7);
		srb.setTerminalSource(0);
		return srb;
	}
	private void verifyDB() throws InterruptedException {
		JUserWapper ju=new JUserWapper();
		int uid=sellerregisterCall.getUserid();
		Logger.comment("验证Userinfo");
		List<Userinfo> lui = ju.selectUserinfoByUid(uid);
		Logger.verifyEquals(1, lui.size(), "验证记录条数");
		Userinfo ui = lui.get(0);
		if(sellerregisterBean.getLoginId()!=null){
			Logger.verifyEquals(sellerregisterBean.getLoginId(), ui.getLoginId(), "验证LoginId");
		}else{
			Logger.verifyNotNull(ui.getLoginId(), "验证LoginId");
		}
		Logger.verifyEquals(sellerregisterBean.getLoginMobile(), ui.getLoginMobile(), "验证LoginMobile");

		Logger.verifyEquals(1, ui.getUserType(), "验证UserType");

		Logger.verifyEquals(0, ui.getUserState(), "验证UserState");
		Logger.verifyEquals(1, ui.getUserSystem(), "验证UserSystem");
		Logger.comment("验证Userinfoex");
		List<Userinfoex> luiex = ju.selectUserinfoexByUid(uid);
		Logger.verifyEquals(1, luiex.size(), "验证记录条数");
		Userinfoex uiex = luiex.get(0);
		//ChannelSource这里都是0表示从码头过来的注册，第三方走userservice
		Logger.verifyEquals(0, uiex.getChannelSource(), "验证ChannelSource");
		Logger.verifyEquals(sellerregisterBean.getRegSource(), uiex.getRegSource(), "验证RegSource");
		if(sellerregisterBean.getTerminalSource()!=null){
			Logger.verifyEquals(sellerregisterBean.getTerminalSource(), uiex.getTerminalSource(), "验证TerminalSource");
		}
		Logger.verifyEquals(sellerregisterBean.getDid(), uiex.getEquimentid(), "验证Equimentid");
		Logger.verifyEquals(sellerregisterBean.getLid(), uiex.getDeviceId(), "验证DeviceId");
		Logger.verifyEquals(sellerregisterBean.getClientIp(), uiex.getClientIp(), "验证ClientIp");
		
		LoginLog loginLog = new JUserWapper().selectLastLoginLogByUid(uid).get(0);
		Logger.debug(FormatUtil.beanToGSONString(loginLog));
		Logger.verifyEquals(ui.getLoginId(), loginLog.getLoginId(), "验证LoginLog表中LoginId");
		Logger.verifyEquals(sellerregisterBean.getClientIp(), loginLog.getClientIp(), "验证LoginLog表中ClientIp");
		// 码头登陆的为0
		Logger.verifyEquals(0, loginLog.getChannelSource(), "验证LoginLog表中ChannelSource");
		Logger.verifyEquals(sellerregisterBean.getTerminalSource(), loginLog.getTerminalSource(), "验证LoginLog表中TerminalSource");
		Logger.verifyEquals(sellerregisterBean.getLid(), loginLog.getDeviceId(), "验证LoginLog表中DeviceId");
		Logger.verifyNotNull(loginLog.getServerIP(), "验证LoginLog表中ServerIP");

		if (sellerregisterCall.getString("code").equals("200")) {
			Logger.verifyIsContains("成功", loginLog.getResponseDesc(), "验证LoginLog表中ResponseDesc");
			Logger.verifyEquals(200, loginLog.getResponseCode(), "验证LoginLog表中ResponseCode");
		} 
		Logger.verifyEquals(1, loginLog.getUserSystem(), "验证LoginLog表中UserSystem");
	}
	private void verifyToken() throws ClientProtocolException, IOException {
		JsonObject result = sellerregisterCall.getJsonObject().get("data").getAsJsonObject();
		String token=result.get("token").getAsString();
		SignAuthBean sab=new SignAuthBean();
		sab.setToken(token);
		Integer suid=JUserCallService.signAuth(sab).getUserId();
		Logger.verifyEquals(suid, sellerregisterCall.getUserid(), "验证Token对应的uid");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerRegister_001() {
		Logger.start(true,"直播微店买手注册成功");
		try {
			sellerregisterBean=initSellerReg();
			sellerregisterCall.setData(sellerregisterBean);
			sellerregisterCall.callService();
			Logger.verifyEquals(200, sellerregisterCall.getCode(), "验证code");
			verifyToken();
			verifyDB();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerRegister_002() {
		Logger.start(true,"不输入loginid直播微店买手注册成功,自动生成loginid");
		try {
			sellerregisterBean=initSellerReg();
			sellerregisterBean.setLoginId(null);
			sellerregisterCall.setData(sellerregisterBean);
			sellerregisterCall.callService();
			Logger.verifyEquals(200, sellerregisterCall.getCode(), "验证code");
			verifyToken();
			verifyDB();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerRegister_003() {
		Logger.start(true,"不输入loginMobile,失败");
		try {
			sellerregisterBean=initSellerReg();
			sellerregisterBean.setLoginMobile(null);
			sellerregisterCall.setData(sellerregisterBean);
			sellerregisterCall.callService();
			Logger.verifyEquals(400, sellerregisterCall.getCode(), "验证code");
			Logger.verifyIsContains("手机号必须填写", sellerregisterCall.getJsonObject().get("message").getAsString(), "验证message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerRegister_004() {
		Logger.start(true,"输入已存在loginMobile,失败");
		try {
			sellerregisterBean=initSellerReg();
			sellerregisterBean.setLoginMobile("13000000001");
			sellerregisterCall.setData(sellerregisterBean);
			sellerregisterCall.callService();
			if(sellerregisterCall.getCode()==200){
				sellerregisterCall.callService();
			}
			Logger.verifyEquals(300, sellerregisterCall.getCode(), "验证code");
			Logger.verifyIsContains("登录手机号已经存在", sellerregisterCall.getJsonObject().get("message").getAsString(), "验证message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerRegister_005() {
		Logger.start(true,"输入码头已存在loginMobile,失败");
		try {
			sellerregisterBean=initSellerReg();
			sellerregisterBean.setLoginMobile("13000000000");
			sellerregisterCall.setData(sellerregisterBean);
			sellerregisterCall.callService();
			if(sellerregisterCall.getCode()==200){
				sellerregisterCall.callService();
			}
			Logger.verifyEquals(300, sellerregisterCall.getCode(), "验证code");
			Logger.verifyIsContains("登录手机号已经存在", sellerregisterCall.getJsonObject().get("message").getAsString(), "验证message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerRegister_006() {
		Logger.start(true,"loginid小于4位失败");
		try {
			sellerregisterBean=initSellerReg();
			sellerregisterBean.setLoginId("ab1");
			sellerregisterCall.setData(sellerregisterBean);
			sellerregisterCall.callService();
			Logger.verifyEquals(400, sellerregisterCall.getCode(), "验证code");
			Logger.verifyIsContains("无效的登陆名", sellerregisterCall.getJsonObject().get("message").getAsString(), "验证message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerRegister_007() {
		Logger.start(true,"loginid大于16位失败");
		try {
			sellerregisterBean=initSellerReg();
			sellerregisterBean.setLoginId("123456abc"+TestDataManager.getRandomNum(10));
			sellerregisterCall.setData(sellerregisterBean);
			sellerregisterCall.callService();
			Logger.verifyEquals(400, sellerregisterCall.getCode(), "验证code");
			Logger.verifyIsContains("无效的登陆名", sellerregisterCall.getJsonObject().get("message").getAsString(), "验证message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerRegister_008() {
		Logger.start(true,"loginid纯数字失败");
		try {
			sellerregisterBean=initSellerReg();
			sellerregisterBean.setLoginId("1234561234567890");
			sellerregisterCall.setData(sellerregisterBean);
			sellerregisterCall.callService();
			Logger.verifyEquals(400, sellerregisterCall.getCode(), "验证code");
			Logger.verifyIsContains("无效的登陆名", sellerregisterCall.getJsonObject().get("message").getAsString(), "验证message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerRegister_009() {
		Logger.start(true,"loginid带特殊字符失败");
		try {
			sellerregisterBean=initSellerReg();
			sellerregisterBean.setLoginId("#"+TestDataManager.getRandomNum(10));
			sellerregisterCall.setData(sellerregisterBean);
			sellerregisterCall.callService();
			Logger.verifyEquals(400, sellerregisterCall.getCode(), "验证code");
			Logger.verifyIsContains("无效的登陆名", sellerregisterCall.getJsonObject().get("message").getAsString(), "验证message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerRegister_010() {
		Logger.start(true,"loginid带敏感词ymatou失败");
		try {
			sellerregisterBean=initSellerReg();
			sellerregisterBean.setLoginId("ymatou1234567890");
			sellerregisterCall.setData(sellerregisterBean);
			sellerregisterCall.callService();
			Logger.verifyEquals(100037, sellerregisterCall.getCode(), "验证code");
			Logger.verifyIsContains("登录Id中存在特殊的不合法的字符", sellerregisterCall.getJsonObject().get("message").getAsString(), "验证message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerRegister_011() {
		Logger.start(true,"password小于6位失败");
		try {
			sellerregisterBean=initSellerReg();
			sellerregisterBean.setPassword("1234");
			sellerregisterCall.setData(sellerregisterBean);
			sellerregisterCall.callService();
			Logger.verifyEquals(400, sellerregisterCall.getCode(), "验证code");
			Logger.verifyIsContains("密码", sellerregisterCall.getJsonObject().get("message").getAsString(), "验证message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerRegister_012() {
		Logger.start(true,"password大于16位失败");
		try {
			sellerregisterBean=initSellerReg();
			sellerregisterBean.setPassword("12345671234567980");
			sellerregisterCall.setData(sellerregisterBean);
			sellerregisterCall.callService();
			Logger.verifyEquals(400, sellerregisterCall.getCode(), "验证code");
			Logger.verifyIsContains("密码", sellerregisterCall.getJsonObject().get("message").getAsString(), "验证message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerRegister_013() {
		Logger.start(true,"直播微店买手注册成功,使用加密密码");
		try {
			sellerregisterBean=initSellerReg();
			sellerregisterBean.setPasswordType(1);
			sellerregisterBean.setPassword( DesUtil.encrypt(JUserCallService.defaultLoginPwd,DesUtil.DES_KEY_STRING,DesUtil.DES_IVPARAMETER));
			sellerregisterCall.setData(sellerregisterBean);
			sellerregisterCall.callService();
			Logger.verifyEquals(200, sellerregisterCall.getCode(), "验证code");
			verifyToken();
			verifyDB();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerRegister_014() {
		Logger.start(true,"直播微店买手注册成功,使用加密密码2");
		try {
			sellerregisterBean=initSellerReg();
			sellerregisterBean.setPasswordType(2);
			byte[] rsabytes = RSAUtils.encryptByPublicKey(sellerregisterBean.getPassword().getBytes(),
					(RSAPublicKey) RSAUtils.loadPublicKey(EnvSetup
							.getData("USER_PUBLIC_KEY")));
			String pasbase64 = Base64.getEncoder().encodeToString(rsabytes);
			sellerregisterBean.setPassword(pasbase64);
			sellerregisterCall.setData(sellerregisterBean);
			sellerregisterCall.callService();
			Logger.verifyEquals(200, sellerregisterCall.getCode(), "验证code");
			verifyToken();
			verifyDB();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}